/*
 * Copyright (C) 2015 Square, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package retrofit2;

import javax.annotation.Nullable;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

/**
 * 适配器，把Call中的泛型适配为返回值中的泛型
 */
public interface CallAdapter<R, T> {
    /**
     * 获取适配后的类型，例如{@code Call<Repo>}，返回的是Repo的Type对象
     */
    Type responseType();

    /**
     * 把Call<R>适配为对应的返回值类型的泛型
     */
    T adapt(Call<R> call);

    /**
     * CallAdapter工厂，每次请求都会通过这个工厂生产CallAdapter
     */
    abstract class Factory {
        /**
         * 对方法进行适配，如果适配不了，返回null
         */
        public abstract @Nullable
        CallAdapter<?, ?> get(Type returnType, Annotation[] annotations, Retrofit retrofit);

        /**
         * 获取泛型的上界类型，例如{@Code Map<String, ? extends Runnable>}，会返回Runnable
         */
        protected static Type getParameterUpperBound(int index, ParameterizedType type) {
            return Utils.getParameterUpperBound(index, type);
        }

        /**
         * 获取原始类型，例如{@code List<? extends Runnable>}，会返回List
         */
        protected static Class<?> getRawType(Type type) {
            return Utils.getRawType(type);
        }
    }
}
